package ruletaPage;

import java.util.List;

import jugador.Jugador;

import org.apache.wicket.ajax.AbstractAjaxTimerBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.time.Duration;

import applications.RuletaApplication;

public class JugadoresEnMesaPanel extends Panel {

	public JugadoresEnMesaPanel(String id) {
		super(id);
		IModel<List<Jugador>> model = new AbstractReadOnlyModel() {
			@Override
			public Object getObject() {
				return RuletaApplication.getInstance().getMesa().getJugadores();
			}
		};
		final ListView<Jugador> jugadores = new ListView<Jugador>("jugadoresEnMesa", model) {
			@Override
			protected void populateItem(ListItem<Jugador> item) {
				item.add(new Label("nombreJugador", new PropertyModel<String>(
						item.getModelObject(), "nombre")));
			}
		};
		jugadores.setOutputMarkupId(true);
        //encapsulate the ListView in a WebMarkupContainer in order for it to update
        WebMarkupContainer listContainer = new WebMarkupContainer("container");
        //generate a markup-id so the contents can be updated through an AJAX call
        listContainer.setOutputMarkupId(true);
        listContainer.add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(1)));
        // add the list view to the container
        listContainer.add(jugadores);
        // finally add the container to the page
        add(listContainer);
	}

}
